package 力扣.数组;

public class 缀点成线1232 {
    public static void main(String[] args) {
//        int[][] ints = {{1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {6, 7}};
//        int[][] ints = {{0, 0},{2,3},{0 ,1}};
//        int[][] ints = {{0, 0},{0,1},{0 ,-1}};
        int[][] ints = {{1, 1},{2,2},{2 ,0}};
        boolean checkStraightLine = checkStraightLine(ints);
//        System.out.println((double)0);
        System.out.println(checkStraightLine);
    }

    /**
     * 通过判断斜率是否一样
     * @param coordinates
     * @return
     */
    public static boolean checkStraightLine(int[][] coordinates) {
        int length = coordinates.length;
        double x = (double) coordinates[0][0];
        double y = (double) coordinates[0][1];
        double tan = 0.0;
        double tan2 = 0.0;
        for (int i = 1; i <length ; i++) {
            double x2 = (double) coordinates[i][0];
            double y2 = (double) coordinates[i][1];
            if (x == x2){//当横坐标相同时，用一个特殊值代替（不要选-1~1之间的数字，因为斜率就是在-1~1的范围内）
                 tan2 = -2;
            }else {
                 tan2 = (y2 - y) / (x2 - x);
            }
            if (tan == 0.0 || tan == tan2){//和上一个斜率进行比较 若相同则比较下一个
                x = x2;
                y = y2;
                tan = tan2;
            }else {
                return false;//若不同则false
            }
        }
        return true;
    }
}
